package com.controller;

import com.dto.BookDTO;
import com.dto.BookStockDTO;
import com.service.BookStockImpl;
import com.service.LoginServiceImpl;
import com.util.Result;
import com.util.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author linyukun
 * @date 2022/7/13 16:29
 */
@RequestMapping("book/stock")
@RestController
public class BookStockController {
    @Autowired
    private LoginServiceImpl loginServiceImpl;

    @Autowired
    private BookStockImpl bookStockImpl;

    /**
     * 保存库存的方法
     * @author linyukun
     * @date 2022-07-09 10:36
     */
    @RequestMapping(value="/saveBookStockByBookId",method = RequestMethod.POST)
    public Result saveBookStockByBookId(@RequestBody BookStockDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setUserName(userName);
            return ResultUtil.returnDataSuccess(bookStockImpl.saveBookStockByBookId(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 删除库存的方法
     * @author linyukun
     * @date 2022-07-09 10:36
     */
    @RequestMapping(value="/delBookStockById",method = RequestMethod.POST)
    public Result delBookStockById(@RequestBody BookStockDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setUserName(userName);
            return ResultUtil.returnDataSuccess(bookStockImpl.delBookStockById(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取图书的库存信息
     * @author linyukun
     * @date 2022-07-14 14：54
     */
    @RequestMapping(value="/getBookStockListByBookId",method = RequestMethod.POST)
    public Result getBookStockListByBookId(@RequestBody BookStockDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(bookStockImpl.getBookStockListByBookId(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    @RequestMapping(value="/getBookStockByTicketNumber",method = RequestMethod.POST)
    public Result getBookStockByTicketNumber(@RequestBody BookStockDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(bookStockImpl.getBookStockByTicketNumber(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

}
